home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / SocketServer.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  10KB  |  327 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. __version__ = '0.4'
  5. import socket
  6. import sys
  7. import os
  8. __all__ = [
  9.     'TCPServer',
  10.     'UDPServer',
  11.     'ForkingUDPServer',
  12.     'ForkingTCPServer',
  13.     'ThreadingUDPServer',
  14.     'ThreadingTCPServer',
  15.     'BaseRequestHandler',
  16.     'StreamRequestHandler',
  17.     'DatagramRequestHandler',
  18.     'ThreadingMixIn',
  19.     'ForkingMixIn']
  20. if hasattr(socket, 'AF_UNIX'):
  21.     __all__.extend([
  22.         'UnixStreamServer',
  23.         'UnixDatagramServer',
  24.         'ThreadingUnixStreamServer',
  25.         'ThreadingUnixDatagramServer'])
  26.  
  27.  
  28. class BaseServer:
  29.     
  30.     def __init__(self, server_address, RequestHandlerClass):
  31.         self.server_address = server_address
  32.         self.RequestHandlerClass = RequestHandlerClass
  33.  
  34.     
  35.     def server_activate(self):
  36.         pass
  37.  
  38.     
  39.     def serve_forever(self):
  40.         while None:
  41.             continue
  42.             return None
  43.  
  44.     
  45.     def handle_request(self):
  46.         
  47.         try:
  48.             (request, client_address) = self.get_request()
  49.         except socket.error:
  50.             return None
  51.  
  52.         if self.verify_request(request, client_address):
  53.             
  54.             try:
  55.                 self.process_request(request, client_address)
  56.             self.handle_error(request, client_address)
  57.             self.close_request(request)
  58.  
  59.         
  60.  
  61.     
  62.     def verify_request(self, request, client_address):
  63.         return True
  64.  
  65.     
  66.     def process_request(self, request, client_address):
  67.         self.finish_request(request, client_address)
  68.         self.close_request(request)
  69.  
  70.     
  71.     def server_close(self):
  72.         pass
  73.  
  74.     
  75.     def finish_request(self, request, client_address):
  76.         self.RequestHandlerClass(request, client_address, self)
  77.  
  78.     
  79.     def close_request(self, request):
  80.         pass
  81.  
  82.     
  83.     def handle_error(self, request, client_address):
  84.         print '-' * 40
  85.         print 'Exception happened during processing of request from', client_address
  86.         import traceback as traceback
  87.         traceback.print_exc()
  88.         print '-' * 40
  89.  
  90.  
  91.  
  92. class TCPServer(BaseServer):
  93.     address_family = socket.AF_INET
  94.     socket_type = socket.SOCK_STREAM
  95.     request_queue_size = 5
  96.     allow_reuse_address = False
  97.     
  98.     def __init__(self, server_address, RequestHandlerClass):
  99.         BaseServer.__init__(self, server_address, RequestHandlerClass)
  100.         self.socket = socket.socket(self.address_family, self.socket_type)
  101.         self.server_bind()
  102.         self.server_activate()
  103.  
  104.     
  105.     def server_bind(self):
  106.         if self.allow_reuse_address:
  107.             self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  108.         
  109.         self.socket.bind(self.server_address)
  110.         self.server_address = self.socket.getsockname()
  111.  
  112.     
  113.     def server_activate(self):
  114.         self.socket.listen(self.request_queue_size)
  115.  
  116.     
  117.     def server_close(self):
  118.         self.socket.close()
  119.  
  120.     
  121.     def fileno(self):
  122.         return self.socket.fileno()
  123.  
  124.     
  125.     def get_request(self):
  126.         return self.socket.accept()
  127.  
  128.     
  129.     def close_request(self, request):
  130.         request.close()
  131.  
  132.  
  133.  
  134. class UDPServer(TCPServer):
  135.     allow_reuse_address = False
  136.     socket_type = socket.SOCK_DGRAM
  137.     max_packet_size = 8192
  138.     
  139.     def get_request(self):
  140.         (data, client_addr) = self.socket.recvfrom(self.max_packet_size)
  141.         return ((data, self.socket), client_addr)
  142.  
  143.     
  144.     def server_activate(self):
  145.         pass
  146.  
  147.     
  148.     def close_request(self, request):
  149.         pass
  150.  
  151.  
  152.  
  153. class ForkingMixIn:
  154.     active_children = None
  155.     max_children = 40
  156.     
  157.     def collect_children(self):
  158.         while self.active_children:
  159.             if len(self.active_children) < self.max_children:
  160.                 options = os.WNOHANG
  161.             else:
  162.                 options = 0
  163.             
  164.             try:
  165.                 (pid, status) = os.waitpid(0, options)
  166.             except os.error:
  167.                 pid = None
  168.  
  169.             if not pid:
  170.                 break
  171.             
  172.             self.active_children.remove(pid)
  173.  
  174.     
  175.     def process_request(self, request, client_address):
  176.         self.collect_children()
  177.         pid = os.fork()
  178.         if pid:
  179.             if self.active_children is None:
  180.                 self.active_children = []
  181.             
  182.             self.active_children.append(pid)
  183.             self.close_request(request)
  184.             return None
  185.         else:
  186.             
  187.             try:
  188.                 self.finish_request(request, client_address)
  189.                 os._exit(0)
  190.             except:
  191.                 
  192.                 try:
  193.                     self.handle_error(request, client_address)
  194.                 finally:
  195.                     os._exit(1)
  196.  
  197.  
  198.  
  199.  
  200.  
  201. class ThreadingMixIn:
  202.     daemon_threads = False
  203.     
  204.     def process_request_thread(self, request, client_address):
  205.         
  206.         try:
  207.             self.finish_request(request, client_address)
  208.             self.close_request(request)
  209.         except:
  210.             self.handle_error(request, client_address)
  211.             self.close_request(request)
  212.  
  213.  
  214.     
  215.     def process_request(self, request, client_address):
  216.         import threading as threading
  217.         t = threading.Thread(target = self.process_request_thread, args = (request, client_address))
  218.         if self.daemon_threads:
  219.             t.setDaemon(1)
  220.         
  221.         t.start()
  222.  
  223.  
  224.  
  225. class ForkingUDPServer(ForkingMixIn, UDPServer):
  226.     pass
  227.  
  228.  
  229. class ForkingTCPServer(ForkingMixIn, TCPServer):
  230.     pass
  231.  
  232.  
  233. class ThreadingUDPServer(ThreadingMixIn, UDPServer):
  234.     pass
  235.  
  236.  
  237. class ThreadingTCPServer(ThreadingMixIn, TCPServer):
  238.     pass
  239.  
  240. if hasattr(socket, 'AF_UNIX'):
  241.     
  242.     class UnixStreamServer(TCPServer):
  243.         address_family = socket.AF_UNIX
  244.  
  245.     
  246.     class UnixDatagramServer(UDPServer):
  247.         address_family = socket.AF_UNIX
  248.  
  249.     
  250.     class ThreadingUnixStreamServer(ThreadingMixIn, UnixStreamServer):
  251.         pass
  252.  
  253.     
  254.     class ThreadingUnixDatagramServer(ThreadingMixIn, UnixDatagramServer):
  255.         pass
  256.  
  257.  
  258.  
  259. class BaseRequestHandler:
  260.     
  261.     def __init__(self, request, client_address, server):
  262.         self.request = request
  263.         self.client_address = client_address
  264.         self.server = server
  265.         
  266.         try:
  267.             self.setup()
  268.             self.handle()
  269.             self.finish()
  270.         finally:
  271.             sys.exc_traceback = None
  272.  
  273.  
  274.     
  275.     def setup(self):
  276.         pass
  277.  
  278.     
  279.     def handle(self):
  280.         pass
  281.  
  282.     
  283.     def finish(self):
  284.         pass
  285.  
  286.  
  287.  
  288. class StreamRequestHandler(BaseRequestHandler):
  289.     rbufsize = -1
  290.     wbufsize = 0
  291.     
  292.     def setup(self):
  293.         self.connection = self.request
  294.         self.rfile = self.connection.makefile('rb', self.rbufsize)
  295.         self.wfile = self.connection.makefile('wb', self.wbufsize)
  296.  
  297.     
  298.     def finish(self):
  299.         if not self.wfile.closed:
  300.             self.wfile.flush()
  301.         
  302.         self.wfile.close()
  303.         self.rfile.close()
  304.  
  305.  
  306.  
  307. class DatagramRequestHandler(BaseRequestHandler):
  308.     
  309.     def setup(self):
  310.         
  311.         try:
  312.             StringIO = StringIO
  313.             import cStringIO
  314.         except ImportError:
  315.             StringIO = StringIO
  316.             import StringIO
  317.  
  318.         (self.packet, self.socket) = self.request
  319.         self.rfile = StringIO(self.packet)
  320.         self.wfile = StringIO()
  321.  
  322.     
  323.     def finish(self):
  324.         self.socket.sendto(self.wfile.getvalue(), self.client_address)
  325.  
  326.  
  327.